package com.beiyou.controller;

import com.beiyou.model.Activity;
import com.beiyou.model.User;
import com.beiyou.service.impl.ActivityServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/UserActivityServlet")
public class UserActivityServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 防止乱码
        req.setCharacterEncoding("utf8");

        String opsCode = req.getParameter("opsCode");

        // 查看全部活动
        if(opsCode.equals("allActivity")){
            ActivityServiceImpl aservice = new ActivityServiceImpl();
            List<Activity> activityList = aservice.getAllActivity();
            // 把查询出来的结果发送到页面上去
            req.setAttribute("activityList",activityList);
            req.getRequestDispatcher("userAllActivity.jsp").forward(req,resp);
        }

        // 查看我的活动
        if(opsCode.equals("myActivity")){
            // 通过点击我的活动时获取会话中该用户名
            HttpSession session = req.getSession(false); // 不创建新会话，过去前端会话信息
            String userName = (String) session.getAttribute("userName");

            User user = new User();
            user.setUsername(userName);
            ActivityServiceImpl aservice = new ActivityServiceImpl();
            List<Activity> activityList = aservice.getMyActivityByCon(user);
            // 把查询出来的结果发送到页面上去
            req.setAttribute("activityList",activityList);
            req.getRequestDispatcher("MyActivityList.jsp").forward(req,resp);
        }

        // 参加活动
        if(opsCode.equals("joinActivity")){
            // 通过点击我的活动时获取会话中该用户名
            HttpSession session = req.getSession(false); // 不创建新会话，过去前端会话信息
            String userName = (String) session.getAttribute("userName");
            String activity_id = (String) req.getParameter("activity_id");

            User user = new User();
            user.setUsername(userName);
            Activity activity = new Activity();
            activity.setId(Integer.parseInt(activity_id));
            ActivityServiceImpl aservice = new ActivityServiceImpl();
            int result = aservice.joinActivity(user,activity);
            if(result>0){
                // 跳转到我的活动页面
                List<Activity> activityList = aservice.getMyActivityByCon(user);
                // 把查询出来的结果发送到页面上去
                req.setAttribute("activityList",activityList);
                req.getRequestDispatcher("MyActivityList.jsp").forward(req,resp);
            }
        }

        // 查询活动
        if(opsCode.equals("search")){
            ActivityServiceImpl aservice = new ActivityServiceImpl();
            String activity_name = req.getParameter("activity_name");
            Activity activity = new Activity();
            activity.setActivity_name(activity_name);
            // 模糊查询操作
            List<Activity> activityList = aservice.getActivityByCon(activity);
            // 刷新活动列表页面
            req.setAttribute("activityList",activityList);
            req.getRequestDispatcher("UTotalActivityList.jsp").forward(req,resp);
            System.out.println("查询活动成功");
            return;
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
